package com.example.demo.maps;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.utils.ChineseCharacterUtil;
import com.example.demo.utils.HttpClientUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 高德地图
 */
public class MapsGdApi {
    private static Logger logger = LoggerFactory.getLogger(MapsGdApi.class);
    /**
     * 获取中国全部城市
     * subdistrict 可选值：0、1、2、3等数字，并以此类推 0：不返回下级行政区； 1：返回下一级行政区； 2：返回下两级行政区；3：返回下三级行政区；
     */
    final static String url="https://restapi.amap.com/v3/config/district";
    //高德key
    final static String key="fe929c592510a02e2d73ad9ea423b22e";


    final static String citys="city";
    public static void getCityAll(){
        List<Map<String,Object>> result=new ArrayList<>();
        JSONObject jsonObject=HttpClientUtils.httpRequest(url,"GET","subdistrict=2&key="+key);
        if(null!=jsonObject){
            //中国-省份、城市
           List<JSONObject> districts= (List<JSONObject>) jsonObject.get("districts");
           if(null!=districts && districts.size()>0){
               for(JSONObject ob:districts){
                   //省份-城市
                   List<JSONObject> disCitylist= (List<JSONObject>) ob.get("districts");
                   logger.info("disCitylist="+disCitylist);
                   if(null!=disCitylist && disCitylist.size()>0){
                      for(JSONObject jsDis:disCitylist){
                          //城市
                          List<JSONObject> cityList= (List<JSONObject>) jsDis.get("districts");
                          if(null!=cityList && cityList.size()>0){
                            for(JSONObject jsCity:cityList){
                                Map<String,Object> data=new HashMap<>();
                                //城市名称
                                String cityName=String.valueOf(jsCity.get("name"));
                                data.put("name",cityName);
                                //城市编码
                                data.put("value",jsCity.get("citycode"));
                                //首字母
                                String zmAll=ChineseCharacterUtil.convertHanzi2Pinyin(cityName,false);
                                data.put("zm",zmAll.substring(0,1));//转换成大写
                                result.add(data);
                                if(cityName.equals("长沙市")){
                                    logger.info("cityName="+cityName);
                                    logger.info("zmAll="+zmAll);
                                    logger.info("data="+data);
                                }

                            }
                          }

                      }
                   }

               }
           }
            result= listOrders(result);
            logger.info("result="+result);
           logger.info("json-result="+ JSON.toJSON(result));
        }
    }

    /**
     * 根据首字母排序
     * @param data
     * @return
     */
    private static List<Map<String,Object>> listOrders(List<Map<String, Object>> data){
        List<Map<String,Object>> orderList=new ArrayList<>();
        String[] orderStr=new String[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        for(int i=0;i<orderStr.length;i++){
            Map<String,Object> strMap=new HashMap<>();
            String str=orderStr[i];
            strMap.put("letter",str);
            List<Map<String,Object>> dataStr=new ArrayList<>();
            for(Map<String,Object> d:data){
                Map<String,Object> city=new HashMap<>();
                String zm=String.valueOf(d.get("zm"));
                if(str.equals(zm)){
                    city.put("name",d.get("name"));
                    city.put("value",d.get("value"));
                    dataStr.add(city);
                }
            }
            strMap.put("data",dataStr);
            orderList.add(strMap);
        }
        return orderList;
    }
}
